/*
 * Copyright 2007. Mount Sinai Hospital, Toronto, Canada.
 * 
 * Licensed under the GNU Lesser General Public License, Version 2. You
 * can find a copy of the license at:
 * 
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
 * 
 * IN NO EVENT SHALL MOUNT SINAI HOSPITAL BE LIABLE TO ANY PARTY FOR DIRECT, 
 * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST 
 * PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, 
 * EVEN IF MOUNT SINAI HOSPITAL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 
 * DAMAGE.
 * 
 * MOUNT SINAI HOSPITAL SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND 
 * ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". 
 * MOUNT SINAI HOSPITAL HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, 
 * UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 
 */
package com.sinai.mshab.client.ui.validate;

import com.sinai.mshab.client.util.Answer;
import com.sinai.mshab.client.ui.basic.BasicFormComponent;

public abstract class Validator {

	/**
	 * An abstract method that needs to be overwritten by every sub
	 * <code>Validator</code> since each validator validates differently.
	 * 
	 * @param component
	 *            The <code>QuestionComponent</code> who's answers shall be
	 *            validated.
	 * @return <code>true</code> if the answer is valid, <code>false</code>
	 *         otherwise.
	 */
	abstract public boolean validate(BasicFormComponent component);

	/**
	 * A generic validator that validates the answers in <code>component</code>
	 * using the regular expression <code>regex</code>.
	 * 
	 * @param component
	 *            The <code>QuestionComponent</code> who's answers shall be
	 *            validated.
	 * @param regex
	 *            A <code>Pattern</code> containing a compiled regex.
	 * @return true if component is valid and false otherwise.
	 */
	public boolean validate(BasicFormComponent component, String regex) {

		boolean valid = false;

		Answer an = component.getAnswer();
		String answer = an.getFirstAnswer();
		return answer.matches(regex);
	}
}
